package com.lancoo.answer.view.fragment.composition.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.lancoo.answer.R;
import com.lancoo.answer.util.DpUtils;
import com.lancoo.answer.widget.ProgressImageView;

import java.util.List;


/**
 * Created by IVAN on 2019/3/27.
 */

public class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private final List<String> imageList;
    private final int type_choice=0;
    private final int type_look=1;
    private ImageOperateCallBack imageOperateCallBack;
    private int showType=0;//0-可作答，1-不可作答，只能查看
    private int maxSize=3;
    private int width,marginWidth;

    public ImageAdapter(List<String> imageList,int showType) {
        this.imageList = imageList;
        this.showType=showType;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    public void setWidth(int width){
        this.width=width;
    }

    public void setMarginWidth(int width){
        this.marginWidth=width;
    }

    public void setImageOperateCallBack(ImageOperateCallBack imageOperateCallBack) {
        this.imageOperateCallBack = imageOperateCallBack;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==type_choice){
            View inflater=LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.ev_item_picture_choice,parent,false);
            return new CameraVH(inflater,imageOperateCallBack);
        }else{
            View inflater=LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.ev_item_picture_show,parent,false);
            return new ShowVH(inflater,imageOperateCallBack,showType,width,marginWidth);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ShowVH){
            ShowVH showVH= (ShowVH) holder;
            showVH.initData(imageList.get(position));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(imageList==null){
            return type_choice;
        }else{
            if(position<imageList.size()){
                return type_look;
            }else{
                return type_choice;
            }
        }
    }

    @Override
    public int getItemCount() {
        if(showType==0){
            return imageList==null?1:(imageList.size()<maxSize?imageList.size()+1:maxSize);
        }else{
            return imageList==null?0:(imageList.size());
        }
    }


    private static class CameraVH extends RecyclerView.ViewHolder implements View.OnClickListener{

        private ImageOperateCallBack imageOperateCallBack;

        public CameraVH(@NonNull View itemView,ImageOperateCallBack imageOperateCallBack) {
            super(itemView);
            this.imageOperateCallBack=imageOperateCallBack;
            itemView.setOnClickListener(this);
            Context mContext=itemView.getContext();
            int widthInPx = DpUtils.getWidthInPx(mContext);
            int dp12=DpUtils.dip2px(mContext,12);
            int dp104=DpUtils.dip2px(mContext,104);
            int width=Math.min((widthInPx-dp12*4)/3,dp104);
            View piv_show = itemView.findViewById(R.id.img_camera);
            ViewGroup.LayoutParams layoutParams = piv_show.getLayoutParams();
            layoutParams.width=width;
            layoutParams.height=width;
            piv_show.setLayoutParams(layoutParams);
        }

        @Override
        public void onClick(View v) {
            if(imageOperateCallBack!=null){
                imageOperateCallBack.gotoImageLibChoice();
            }

        }
    }

    private static class ShowVH extends RecyclerView.ViewHolder implements View.OnClickListener{

        private ImageOperateCallBack imageOperateCallBack;
        private final RoundedCorners roundedCorners;
        private final RequestOptions options;

        public ShowVH(@NonNull View itemView, ImageOperateCallBack imageOperateCallBack,
                      int showType,int showWidth,int marginWidth) {
            super(itemView);
            this.imageOperateCallBack=imageOperateCallBack;
            itemView.findViewById(R.id.img_delet).setOnClickListener(this);
            itemView.findViewById(R.id.piv_show).setOnClickListener(this);
            itemView.findViewById(R.id.view_delete).setOnClickListener(this);
            Log.e("CameraVH","CameraVH:"+itemView.getWidth());
            roundedCorners =
                    new RoundedCorners(3);
            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
            options = RequestOptions.bitmapTransform(roundedCorners)
                    .transforms(new CenterCrop(), roundedCorners)
                    .override(130,130)
                    .placeholder(R.mipmap.ev_ic_picture_default)
                    .error(R.mipmap.ev_ic_picture_default);
            Context mContext=itemView.getContext();
            int widthInPx = (showWidth==0?DpUtils.getWidthInPx(mContext):showWidth);
            int dp4=DpUtils.dip2px(mContext,4);
            int dp12=DpUtils.dip2px(mContext,12);
            int dp104=DpUtils.dip2px(mContext,104);
            int width=Math.min((widthInPx-dp12*4-dp4)/3,dp104);
            View piv_show = itemView.findViewById(R.id.piv_show);
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) piv_show.getLayoutParams();
            layoutParams.width=width;
            layoutParams.height=width;
            if(marginWidth!=0){
                layoutParams.setMarginEnd(marginWidth);
            }
            piv_show.setLayoutParams(layoutParams);
            itemView.findViewById(R.id.rl_delet).setVisibility(showType==0?View.VISIBLE:View.GONE);
            itemView.findViewById(R.id.view_delete).setVisibility(showType==0?View.VISIBLE:View.GONE);
        }

        @Override
        public void onClick(View v) {
            int adapterPosition =getBindingAdapterPosition();
            Log.e("imgO","点击le"+adapterPosition);
            if(adapterPosition<0||imageOperateCallBack==null){
                return;
            }
            int id = v.getId();
            if(id==R.id.img_delet||id==R.id.view_delete){
                Log.e("imgO","点击le");
                imageOperateCallBack.deletChoiceImage(adapterPosition);
            }else if(id==R.id.piv_show){
                imageOperateCallBack.lookChoiceImage(adapterPosition);
            }

        }

        private void initData(String url){
            Log.e("CameraVH","initData:"+itemView.getWidth());
            if(url==null){
                return;
            }
            final ProgressImageView progressImageView=itemView.findViewById(R.id.piv_show);
            Glide.with(progressImageView)
                    .load(url.trim())
                    .apply(options)
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            progressImageView.stopProgress();
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            progressImageView.stopProgress();
                            return false;
                        }

                    }).into(progressImageView);
        }
    }

    public abstract static class ImageOperateCallBack{

        public void gotoImageLibChoice() {
        }

        public void deletChoiceImage(int position){}

        public void lookChoiceImage(int position){}

    }

}